package com.service.store.impl;

import com.dao.store.CompanyDao;
import com.dao.store.CourseDao;
import com.domain.store.Company;
import com.domain.store.Course;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.store.CourseService;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CourseServiceImpl implements CourseService {
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Course> findAll() {
        List<Course> list = null;
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            list = mapper.findAll();
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size) {
        PageInfo pageInfo = null;
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            Page pages = PageHelper.startPage(page,size);
            mapper.findAll();
            pageInfo = new PageInfo(pages);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Course findById(String id) {
        Course course = null;
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            course = mapper.findById(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return course;
    }

    @Override
    public void save(Course course) {
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            String id = UUID.randomUUID().toString();
            //后台自动添加当前时间
            course.setCreateTime(new Date());
            course.setId(id);
            mapper.save(course);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Course course) {
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            mapper.delete(course);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Course course) {
        try {
            SqlSession session= MapperUtils.init();
            CourseDao mapper = session.getMapper(CourseDao.class);
            mapper.update(course);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
